import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
import "java.io.*"
import "android.graphics.*"
import "android.content.res.ColorStateList"
import "android.webkit.*"
import "android.content.*"
import "android.net.*"
if LUA_SD==nil
  LUA_SD="/sdcard/"
end
btBackground=Button(activity).Background .setColorFilter(PorterDuffColorFilter(0xff59cfc9,PorterDuff.Mode.SRC_ATOP)).setColor( ColorStateList( int[0].class{int{}}, int{0xde9610ff}))
function ChoicePath(StartPath,callback)
  --创建ListView作为文件列表
  lv=ListView(activity).setFastScrollEnabled(true)
  --创建路径标签
  cp=TextView(activity)
  lay=LinearLayout(activity).setOrientation(1).addView(cp).addView(lv)
  ChoiceFile_dialog=AlertDialog.Builder(activity) .setTitle("选择路径") .setPositiveButton("OK",{ onClick=function() callback(tostring(cp.Text)) end}) .setNegativeButton("Canel",nil) .setView(lay) .show()
  adp=ArrayAdapter(activity,android.R.layout.simple_list_item_1)
  lv.setAdapter(adp)
  function SetItem(path)
    path=tostring(path)
    adp.clear()
    cp.Text=tostring(path)
    if path~="/" then
      adp.add("../")
    end
    ls=File(path).listFiles()
    if ls~=nil then
      ls=luajava.astable(File(path).listFiles()) --全局文件列表变量
      table.sort(ls,function(a,b)
        return (a.isDirectory()~=b.isDirectory() and a.isDirectory()) or ((a.isDirectory()==b.isDirectory()) and a.Name<b.Name)
      end)
     else
      ls={}
    end
    for index,c in ipairs(ls) do
      if c.isDirectory() then--如果是文件夹则
        adp.add(c.Name.."/")
      end
    end
  end
  lv.onItemClick=function(l,v,p,s)--列表点击事件
    项目=tostring(v.Text)
    if tostring(cp.Text)=="/" then
      路径=ls[p+1]
     else
      路径=ls[p]
    end
    if 项目=="../" then
      SetItem(File(cp.Text).getParentFile())
     elseif 路径.isDirectory() then
      SetItem(路径)
     elseif 路径.isFile() then
      callback(tostring(路径))
      ChoiceFile_dialog.hide()
    end
  end
  SetItem(StartPath)
end

function ChoiceFile(StartPath,callback)
  lv=ListView(activity).setFastScrollEnabled(true)
  cp=TextView(activity)
  lay=LinearLayout(activity).setOrientation(1).addView(cp).addView(lv)
  ChoiceFile_dialog=AlertDialog.Builder(activity) .setTitle("选择文件") .setView(lay) .show()
  adp=ArrayAdapter(activity,android.R.layout.simple_list_item_1)
  lv.setAdapter(adp)
  function SetItem(path)
    path=tostring(path)
    adp.clear()
    cp.Text=tostring(path)
    if path~="/" then
      adp.add("../")
    end
    ls=File(path).listFiles()
    if ls~=nil then
      ls=luajava.astable(File(path).listFiles())
      table.sort(ls,function(a,b)
        return (a.isDirectory()~=b.isDirectory() and a.isDirectory()) or ((a.isDirectory()==b.isDirectory()) and a.Name<b.Name)
      end)
     else
      ls={}
    end
    for index,c in ipairs(ls) do
      if c.isDirectory() then
        adp.add(c.Name.."/")
       else
        adp.add(c.Name)
      end
    end
  end
  lv.onItemClick=function(l,v,p,s)
    项目=tostring(v.Text)
    if tostring(cp.Text)=="/" then
      路径=ls[p+1]
     else
      路径=ls[p]
    end
    if 项目=="../" then
      SetItem(File(cp.Text).getParentFile())
     elseif 路径.isDirectory() then
      SetItem(路径)
     elseif 路径.isFile() then
      callback(tostring(路径))
      ChoiceFile_dialog.hide()
    end
  end
  SetItem(StartPath)
end
function init()
  if not File(LUA_SD.."/avHandle/").exists()
    os.execute("mkdir "..LUA_SD.."/avHandle/")
  end
  if not File(LUA_SD.."/avHandle/output/").exists()
    os.execute("mkdir "..LUA_SD.."/avHandle/output/")
  end
  if not File(LUA_SD.."/avHandle/tmp/").exists()
    os.execute("mkdir "..LUA_SD.."/avHandle/tmp/")
  end
  local file=File(tostring(activity.getFilesDir()).."/ffmpeg")
  if not file.exists()
    import "android.content.DialogInterface"
    local d = ProgressDialog.show(this, "提示", "正在下载依赖文件",false, false, nil)
    Http.download("http://cdn1.cxoip.com/libs/AndroLua/assets/armeabi-v7a/ffmpeg" ,LUA_SD.."/avHandle/ffmpeg",nil,nil,function(a,t)
      if a==200
        d.hide()
        print("下载完毕，请重新启动")
        LuaUtil.copyDir(LUA_SD.."/avHandle/ffmpeg",tostring(activity.getFilesDir()).."/ffmpeg")
        file.setExecutable(true)
        os.remove(LUA_SD.."/avHandle/ffmpeg")
        task(1000,function()
          os.exit()
        end)
      end
    end)
  end
end
function dialogPlayerVideo(context,dir)
  local video=
  {
    LinearLayout;
    layout_width="fill";
    background="0";
    orientation="vertical";
    layout_height="40%h";
    gravity="center";
    {
      VideoView;
      layout_width="fill";
      layout_height="40%h";
      id="vi";
    };
    {
      LinearLayout;
      layout_width="-1";
      {
        TextView;
        id="t";
        text="00:00";
      };
      {
        SeekBar;
        layout_width="-1";
        id="sb";
        style="?android:attr/progressBarStyleHorizontal";
      };
    };
  };
  local ti=Ticker()
  ti.Period=500
  ti.onTick=function()
    local s=vi.getCurrentPosition()/1000
    local m=math.floor(s/60)
    t.text=tostring(m)..":".. tostring(s-m*60)
    sb.Max=vi.getDuration()
    sb.progress= vi.getCurrentPosition()
  end
  AlertDialog.Builder(context).setView(loadlayout(video)).setPositiveButton("关闭",{onClick=function()
      ti.stop()
  end}).show().setCanceledOnTouchOutside(false) .setCancelable(false)
  vi.setVideoURI(Uri.parse(dir)).start().setMediaController(MediaController(context))
  ti.start()
  sb.setOnSeekBarChangeListener{
    onStopTrackingTouch=function()
      vi.seekTo(sb.progress)
  end}
end
function dialogPlayer(dir)
  import "android.media.*"
  local audio={
    LinearLayout;
    layout_width="fill";
    orientation="vertical";
    gravity="center";
    layout_height="fill";
    {
      TextView;
      text="歌名";
      textSize="20sp";
      textColor="0xff000000";
      id="songname";
    };
    {
      RelativeLayout;
      gravity="center";
      layout_width="-1";
      {
        TextView;
        gravity="center";
        id="prog";
        text="进度";
        layout_height="50dp";
      };
      {
        TextView;
        text="暂停";
        layout_alignParentRight="true";
        style="?android:attr/buttonBarButtonStyle";
        id="control";
        layout_width="50dp";
        layout_height="50dp";
      };
      {
        LinearLayout;
        layout_width="-1";
        layout_toRightOf="prog";
        gravity="center";
        id="sb_ll";
        layout_toLeftOf="control";
        layout_height="50dp";
        {
          SeekBar;
          style="?android:attr/progressBarStyleHorizontal";
          id="sb";
          layout_marginLeft="10dp";
          layout_width="-1";
        };
      };
    };
  }
  local mp=MediaPlayer()
  local ti=Ticker()
  ti.Period=500
  ti.onTick=function()
    sb.progress=mp.getCurrentPosition()
    prog.text= tostring( math.floor((sb.progress/1000)/60))..":".. tostring(math.floor(sb.progress/1000- math.floor((sb.progress/1000)/60)*60)) .."/".. tostring( math.floor((sb.Max/1000)/60))..":".. tostring(math.floor(sb.Max/1000- math.floor((sb.Max/1000)/60)*60))
  end
  mp.reset().setDataSource(dir) .prepare().start()
  ti.start()
  AlertDialog.Builder(this).setView(loadlayout(audio)).setPositiveButton("关闭",{onClick=function()
      mp.stop()
      ti.stop()
      mp=nil
  end}).show() .setCanceledOnTouchOutside(false) .setCancelable(false)
  sb.Max=mp.getDuration()
  control. onClick=function()
    if control.text=="暂停"
      control.text="播放"
      mp.pause()
     else
      control.text="暂停"
      mp.start()
    end
  end
  sb.setOnSeekBarChangeListener{
    onStopTrackingTouch=function()
      mp.seekTo(sb.progress)
  end}
  songname.text=File(dir).getName()
end